package cn.cau.epidemicsurveysystem.control;

import cn.cau.epidemicsurveysystem.bean.StaffInfoBean;
import cn.cau.epidemicsurveysystem.service.StaffInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@RestController
public class WechatUserController {
    @Autowired
    StaffInfoService staffInfoService;
    //小程序-登陆
    @RequestMapping(value = "/wechatLogin",method = RequestMethod.POST)
    public  Map<String, Object> wechatLogin(@RequestParam("staffNo") String staffNo, @RequestParam("staffPwd") String staffPwd, HttpSession session){
        StaffInfoBean staffInfo = staffInfoService.findStaffByNo(staffNo);
        Map<String, Object> map = new HashMap<String, Object>();
        //status字段用于指示用户的登录状态
        //0-用户名错误
        //1-密码错误
        //2-登录成功
        if(staffInfo==null){   //用户名错误
            map.put("status",0);
        }
        else{
            if(!staffInfo.getStaffPwd().equals(staffPwd)){    //密码错误
                map.put("status",1);
            }
            else{
                map.put("status",2);
                map.put("staffNo", staffInfo.getStaffNo());
            }
        }
        return map;
    }
    //小程序-获取用户信息
    @RequestMapping(value = "/wechatStaffInfo",method = RequestMethod.GET)
    public  Map<String, Object> getWechatStaffInfo(@RequestParam("staffNo") String staffNo,HttpSession session){
        StaffInfoBean staffInfo = staffInfoService.findStaffByNo(staffNo);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("staffNo",staffInfo.getStaffNo());
        map.put("staffName",staffInfo.getStaffName());
        map.put("staffPwd",staffInfo.getStaffPwd());
        map.put("staffTele",staffInfo.getStaffTele());
        map.put("staffPosition",staffInfo.getStaffPosition());
        map.put("staffWorkArea",staffInfo.getStaffWorkArea());
        map.put("staffWorkplace",staffInfo.getStaffWorkplace());
        map.put("staffWorkState",staffInfo.getStaffWorkState());
        return map;
    }
    //小程序-修改用户信息
    @RequestMapping(value = "/wechatStaffInfoUpdate",method = RequestMethod.POST)
    public  boolean wechatStaffInfoUpdate(@RequestParam("staffNo") String staffNo,
                                                      @RequestParam("staffPwd") String staffPwd,
                                                      @RequestParam("staffName") String staffName,
                                                      @RequestParam("staffTele") String staffTele,
                                                      @RequestParam("staffPosition") String staffPosition,
                                                      @RequestParam("staffWorkArea") String staffWorkArea,
                                                      @RequestParam("staffWorkplace") String staffWorkplace,
                                                      @RequestParam("staffWorkState") String staffWorkState,
                                                      HttpSession session){
        StaffInfoBean staffInfo = staffInfoService.findStaffByNo(staffNo);
        staffInfo.setStaffName(staffName);
        staffInfo.setStaffPwd(staffPwd);
        staffInfo.setStaffTele(staffTele);
        staffInfo.setStaffPosition(staffPosition);
        staffInfo.setStaffWorkArea(staffWorkArea);
        staffInfo.setStaffWorkplace(staffWorkplace);
        staffInfo.setStaffWorkState(Integer.valueOf(staffWorkState));
        return staffInfoService.updateStaffInfo(staffInfo);
    }
}
